jetcrab\bytecode\scope/
constants.rs1use crate::vm::types::ConstantIndex;
2use std::collections::HashMap;
3
4pub trait ConstantManager {
5 fn add_constant(&mut self, value: String) -> ConstantIndex;
6 fn get_constants(&self) -> &Vec<String>;
7}
8
9pub trait ConstantCore {
10 fn constants(&self) -> &Vec<String>;
11 fn constant_map(&self) -> &HashMap<String, ConstantIndex>;
12 fn constants_mut(&mut self) -> &mut Vec<String>;
13 fn constant_map_mut(&mut self) -> &mut HashMap<String, ConstantIndex>;
14}
15
16impl<T> ConstantManager for T
17where
18 T: ConstantCore,
19{
20 fn add_constant(&mut self, value: String) -> ConstantIndex {
21 if let Some(&id) = self.constant_map().get(&value) {
22 id
23 } else {
24 let id = ConstantIndex::new(self.constants().len());
25 self.constants_mut().push(value.clone());
26 self.constant_map_mut().insert(value, id);
27 id
28 }
29 }
30
31 fn get_constants(&self) -> &Vec<String> {
32 self.constants()
33 }
34}